//
//  LoginViewController.m
//  NewApp
//
//  Created by  on 12-6-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"
#import "MemberRegisterViewController.h"
#import "RetrivePasswordViewController.h"
#import "NAAccount.h"
#import "PathHelper.h"
#import "MemberShipViewController.h"


@interface LoginViewController ()

/**
 *  Login request did finished
 */
- (void) loginRequestDidFinished:(RequestEngine *) engine withObject:(id) object;

/**
 *  Goto member home page
 */
- (void) goMemberHomePage;


@end

@implementation LoginViewController

@synthesize scrollView,accountTextfield,passwordTextfield;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.scrollView.contentSize = CGSizeMake(320.f, 480.f);
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    [self.passwordTextfield setSecureTextEntry:YES];
    
    [self.engine loginInfos];
}

- (void) dealloc {
    if (loginEngin != nil) {
        [loginEngin setDelegate:nil];
        [loginEngin release];
        loginEngin = nil;
    }
    [super dealloc]; 
}

#pragma mark -
#pragma mark - UITextField delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark -
#pragma mark - Override methods

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *) object;
        [dict retain];
        
        NSString *title = [dict objectForKey:@"top_title"];
        if (title != nil && title.length > 0)
            self.titleLabel.text = title;
        
        [dict release];
        
    }
}

#pragma mark -
#pragma mark - Private methods

- (void) loginRequestDidFinished:(RequestEngine *) engine withObject:(id) object {
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *) object;
        [dict retain];
        
        if ([[dict objectForKey:@"status"] isEqualToString:@"1"]) {
            NSArray *dataArray = [dict objectForKey:@"data"];
            if (dataArray != nil && dataArray.count > 0) {
                NAAccount *acc = [[NAAccount alloc] initWithDictionary:[dataArray objectAtIndex:0]];
                NSString *filePath = [[PathHelper documentDirectoryPathWithName:@"Acc"] 
                                      stringByAppendingPathComponent:@"accounts.info"];
                [NSKeyedArchiver archiveRootObject:acc toFile:filePath];
                
                [self goMemberHomePage];
            }
        }else {
            [NAError showAlertMessageWithTitle:@"" message:[dict objectForKey:@"msg"]];
        }
        
        [dict release];
    }
    else {
        [[NAError errorWithCode:NAError_REG_FAIL] showErrorMessage:nil];
    }
    
    NA_RELEASE_SAFELY(loginEngin);
}

- (void) goMemberHomePage {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark - Event handler methods

- (IBAction) RegisterButtonPressed:(id)sender {
    MemberRegisterViewController *viewCtrl = [[MemberRegisterViewController alloc] initWithNibName:@"MemberRegisterViewController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:viewCtrl animated:YES];
    [viewCtrl release];
}

- (IBAction) forgetPasswordButtonPressed:(id)sender {
    RetrivePasswordViewController *viewCtrl = [[RetrivePasswordViewController alloc] initWithNibName:@"RetrivePasswordViewController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:viewCtrl animated:YES];
    [viewCtrl release];
}

- (IBAction) loginButtonPressed:(id)sender {
    if (self.accountTextfield.text.length  == 0 || self.passwordTextfield.text == 0) {
        [NAError showAlertMessageWithTitle:@"" 
                                   message:@"用戶名或者密碼不能為空"];
        return;
    }
    
    if (loginEngin != nil) 
        return;
    
    loginEngin = [[RequestEngine alloc] initWithTarget:self action:@selector(loginRequestDidFinished:withObject:)];
    [loginEngin loginWithUserName:self.accountTextfield.text password:self.passwordTextfield.text];
}




@end
